package com.suge.order.manager.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.suge.common.query.order.SmsOrderQueryDO;
import com.suge.common.result.order.OrderResult;
import com.suge.order.dao.SmsOrderDAO;
import com.suge.order.dao.UserBizOrderDAO;
import com.suge.order.dao.dataobject.SmsOrderDO;
import com.suge.order.dao.dataobject.UserBizOrderDO;
import com.suge.order.manager.SmsOrderManager;

public class SmsOrderManagerImpl implements SmsOrderManager {

	private static final Logger logger = LoggerFactory.getLogger(SmsOrderManagerImpl.class);

	@Autowired
	private UserBizOrderDAO userBizOrderDAO;
	@Autowired
	private SmsOrderDAO smsOrderDAO;
	
	public SmsOrderDO getSmsOrderByOrderId(long orderId) {
		SmsOrderDO smsOrderDO = smsOrderDAO.getSmsOrderByOrderId(orderId);
		return smsOrderDO;
	}

	public OrderResult getSmsOrderListByUserId(long userId) {
		List<SmsOrderDO> smsOrderDOs = smsOrderDAO.getSmsOrderListByUserId(userId); 
		OrderResult orderResult = new OrderResult();
		orderResult.setSuccess(false);
		if(smsOrderDOs!=null && smsOrderDOs.size()>0){
			orderResult.setSuccessList(smsOrderDOs);
			orderResult.setSuccess(true);
		}
		return orderResult;
	}

	public OrderResult getSmsOrderListByUserId(SmsOrderQueryDO params) {
		List<SmsOrderDO> smsOrderDOs = smsOrderDAO.getSmsOrderByQuery(params); 
		OrderResult orderResult = new OrderResult();
		orderResult.setSuccess(false);
		if(smsOrderDOs!=null && smsOrderDOs.size()>0){
			orderResult.setSuccessList(smsOrderDOs);
			orderResult.setSuccess(true);
		}
		return orderResult;
	}
	
	public OrderResult getSmsOrderListByUserNick(String userNick) {
		List<SmsOrderDO> smsOrderDOs =smsOrderDAO.getSmsOrderListByUserNick(userNick);
		OrderResult orderResult = new OrderResult();
		orderResult.setSuccess(false);
		if(smsOrderDOs!=null && smsOrderDOs.size()>0){
			orderResult.setSuccessList(smsOrderDOs);
			orderResult.setSuccess(true);
		}
		return orderResult;
	}

	public OrderResult updateSmsOrderStatus(SmsOrderDO smsOrderDO) {
		OrderResult orderResult = new OrderResult();
		orderResult.setSuccess(false);
		boolean updateRes = smsOrderDAO.updateSmsOrderStatus(smsOrderDO);
		if(updateRes){
			orderResult.setSuccess(true);
		}
		return orderResult;
	}

	public OrderResult updateSmsOrderInfo(SmsOrderDO smsOrderDO) {
		OrderResult orderResult = new OrderResult();
		orderResult.setSuccess(false);
		boolean updateRes =smsOrderDAO.updateSmsOrderInfo(smsOrderDO);
		if(updateRes){
			orderResult.setSuccess(true);
		}
		return orderResult;
	}

	public OrderResult insertSmsOrder(SmsOrderDO smsOrderDO) {
		logger.error(")))))))))))))))))))))))))))))))))))))))))))))))))--");
		OrderResult orderResult = new OrderResult();
		orderResult.setSuccess(false);
		if(smsOrderDO==null || smsOrderDO.getUserNick()==null || smsOrderDO.getOrderType()==-1){
			orderResult.setErrorMessage("Order message is null  or invalid");
			logger.error(")))))))))))))))))))))))))))))))))))))))))))))))))");
			return orderResult;
		}
		UserBizOrderDO userBizOrderDO = userBizOrderDAO.getUserBizOrderByUserId(smsOrderDO.getUserId());
		if(userBizOrderDO==null || userBizOrderDO.getMessageInfo()==null || userBizOrderDO.getValidMessageCount()<=0){
			orderResult.setErrorMessage("User has not message order or out of service");
			logger.error(")))))))))))))))))))))))))))))))))))))))))))))))))");
			return orderResult;
		}
		userBizOrderDO.setValidMessageCount(userBizOrderDO.getValidMessageCount()-1);
		userBizOrderDO.setMessageSentCount(userBizOrderDO.getMessageSentCount()+1);
		boolean reduceCount  = userBizOrderDAO.updateUserBizOrderInfo(userBizOrderDO);
		smsOrderDO.setOuterOrderId(smsOrderDO.getUserBizorderId()+"-"+smsOrderDO.getOrderType());
		boolean isNotExisted  = smsOrderDAO.getSmsOrderByOuterOrderId(smsOrderDO.getOuterOrderId()) ==null ? true:false;
		logger.error(")))))))))))))))))))))))))))))))))))))))))))))))))----"+reduceCount+"-"+isNotExisted);
		
		if(reduceCount && isNotExisted){
			smsOrderDAO.insertSmsOrder(smsOrderDO);
			orderResult.getSuccessList().add(smsOrderDO);
			orderResult.setSuccess(true);
			return orderResult;
		}
		
		return orderResult;
	}

	public void setUserBizOrderDAO(UserBizOrderDAO userBizOrderDAO) {
		this.userBizOrderDAO = userBizOrderDAO;
	}

	public UserBizOrderDAO getUserBizOrderDAO() {
		return userBizOrderDAO;
	}

	public void setSmsOrderDAO(SmsOrderDAO smsOrderDAO) {
		this.smsOrderDAO = smsOrderDAO;
	}

	public SmsOrderDAO getSmsOrderDAO() {
		return smsOrderDAO;
	}

	public List<SmsOrderDO> getSmsOrderByQuery(SmsOrderQueryDO smsOrderQueryDO) {
		return smsOrderDAO.getSmsOrderByQuery(smsOrderQueryDO);
	}

}
